package lumis.portal.webservice;

import lumis.portal.AccessDeniedException;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.util.ITransaction;

/**
 * Utility class for Web Services implementations.
 *
 * @version $Revision: 5769 $ $Date: 2007-02-01 12:00:07 -0200 (Thu, 01 Feb 2007) $
 * @since 4.0.9
 */
public abstract class WebServiceUtil
{
	/**
	 * Get a {@link SessionConfig} object for a sessionId.
	 * @param sessionId
	 * @param transaction
	 * @return {@link SessionConfig} object for the specified sessionId.
	 * @throws PortalException
	 */
	public static SessionConfig getSessionConfig(String sessionId, ITransaction transaction) throws PortalException
	{
		SessionConfig sessionConfig = new SessionConfig();
		sessionConfig.setUserSessionId(sessionId);
		if(ManagerFactory.getAuthenticationManager().validateSessionId(sessionConfig, transaction))
			return sessionConfig;
		
		throw new AccessDeniedException();
	}
	
	/**
	 * Converts zero-length strings to null. Used by code that reads web service response objects, that come with null string values as zero-length strings.
	 * @param value string value to be converted
	 * @return value converted
	 */
	public static String zeroLengthToNull(String value)
	{
		return (value == null || value.length() == 0 ? null : value);
	}
}
